﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using Models;
using Common;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;

namespace DbControl
{
    public class OprWarndatas
    {
        public static int Insert(warningdatas data)
        {
            try
            {
                using (IDbConnection connection = new SqlConnection(OprSql.connectString))
                {
                    string sql = @"INSERT INTO warningdatas
                                   (sensordata_id 
                                   ,sensor_sn 
                                   ,device_code 
                                   ,device_name 
                                   ,parameter_code 
                                   ,value 
                                   ,warn_time 
                                   ,status 
                                   ,[user_id]
                                   ,remove_time 
                                   ,statu 
                                   ,flag
                                   ,vis
                                   ,vis40
                                  ,temper
                                  ,dens
                                  ,diel
                                  ,ppm
                                  ,aw
                                  ,mo
                                  ,fe
                                  ,nfe
                                  ,iso)
                             VALUES
                                   (@sensordata_id
                                   ,@sensor_sn
                                   ,@device_code
                                   ,@device_name
                                   ,@parameter_code
                                   ,@value
                                   ,@warn_time
                                   ,@status
                                   ,@user_id
                                   ,@remove_time
                                   ,@statu
                                   ,@flag
                                   ,@vis
                                   ,@vis40
                                   ,@temper
                                   ,@dens
                                   ,@diel
                                   ,@ppm
                                   ,@aw
                                   ,@mo
                                   ,@fe
                                   ,@nfe
                                   ,@iso)";
                    return connection.Execute(sql, data);
                }
            }
            catch (Exception ex)
            {
                lLogWrite.WriteLog(ex.ToString());
                return 0;
            }
        }
        public static DataTable GeAlarmsByName(DateTime d1, DateTime d2, string Name)
        {
            using (IDbConnection connection = new SqlConnection(OprSql.connectString))
            {
                string sql = @"SELECT id  
                              ,warn_time 
                              ,sensor_sn 
                              ,device_code 
                              ,device_name 
                              ,parameter_code
                              ,statu 
                              ,flag 
                              ,vis 
                              ,vis40 
                              ,temper 
                              ,diel 
                              ,ppm 
                              ,aw 
                              ,mo 
                              ,fe 
                              ,nfe 
                              ,iso 
                              ,dens 
                          FROM  warningdatas  WHERE warn_time between @t1 and @t2 order by id desc";
                if (!string.IsNullOrEmpty(Name))
                {
                    sql = @"SELECT id  
                              ,warn_time 
                              ,sensor_sn 
                              ,device_code 
                              ,device_name 
                              ,parameter_code
                              ,statu 
                              ,flag 
                              ,vis 
                              ,vis40 
                              ,temper 
                              ,diel 
                              ,ppm 
                              ,aw 
                              ,mo 
                              ,fe 
                              ,nfe 
                              ,iso 
                              ,dens 
                          FROM warningdatas  WHERE warn_time between @t1 and @t2  and device_name=@DeviceName order by id asc";
                }
                DataTable dt = new DataTable();
                dt.Load(connection.ExecuteReader(sql, new { t1 = d1, t2 = d2, DeviceName = Name }));
                return dt;                
            }
        }
    }
}
